<template>
  <div class="app-container">
    <el-card>
      <el-form ref="searchForm" :model="searchForm" inline size="small">
        <el-form-item label="ID">
          <el-input type="number" v-model="searchForm.id" placeholder="请输入Id" clearable />
        </el-form-item>
        <el-form-item label="文章标题">
          <el-input
            v-model="searchForm.title"
            placeholder="请输入文章标题"
            clearable
          />
        </el-form-item>
        <el-form-item label="状态">
          <el-select
            v-model="searchForm.publishStatus"
            placeholder="请选择状态"
            clearable
            size="small"
          >
            <el-option label="全部" value=""></el-option>
            <el-option label="未发布" value="0"></el-option>
            <el-option label="已发布" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发布账号">
          <el-select
            v-model="searchForm.linkAccountId"
            placeholder="请选择发布账号"
            clearable
            size="small"
          >
            <el-option
              v-for="item in linkAccountList"
              :key="item.id"
              :label="item.officialAuthnName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="发布时间" prop="startTime">
          <el-date-picker
            style="width: 200px"
            v-model="searchForm.beginTime"
            align="right"
            type="date"
            placeholder="选择开始日期"
            :picker-options="pickerOptions"
            value-format="yyyy-MM-dd"
            format="yyyy 年 MM 月 dd 日"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>-</el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="searchForm.endTime"
            align="right"
            type="date"
            placeholder="选择结束日期"
            :picker-options="pickerOptions"
            value-format="yyyy-MM-dd"
            format="yyyy 年 MM 月 dd 日"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="文章分类"
          ><el-cascader
            v-model="searchForm.cateId"
            :options="cateList"
            :props="props"
            clearable
            size="small"
          />
        </el-form-item>
        <el-form-item label="是否置顶">
          <el-select
            v-model="searchForm.topStatus"
            placeholder="请选择"
            clearable
            size="small"
          >
            <el-option label="全部" value=""></el-option>
            <el-option label="未置顶" value="0"></el-option>
            <el-option label="已置顶" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-refresh" @click="resetForm('searchForm')"
            >重置</el-button
          >
          <el-button icon="el-icon-search" type="primary" @click="getList()"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
      <div>
        <el-button
          size="small"
          type="primary"
          @click="addArtice()"
          icon="el-icon-plus"
          >添加文章</el-button
        >
        <el-button size="small" @click="deleteMany()" icon="el-icon-delete"
          >批量删除</el-button
        >
      </div>

      <el-table
        :data="tableData.data"
        v-loading="listLoading"
        border
        style="width: 100%; margin-top: 20px"
        @selection-change="selectTableList"
        size="small"
      >
        <el-table-column align="center" type="selection" width="55" />
        <el-table-column align="center" prop="id" label="ID" width="80" />
        <el-table-column align="center" prop="title" label="文章标题" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <span>《 {{ scope.row.title }} 》</span>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="cateName" label="文章分类" />
        <el-table-column align="center" prop="nickname" label="发布账号" />
        <el-table-column align="center" prop="operator" label="发布人" />
        <el-table-column align="center" prop="publishTime" label="发布时间" />

        <el-table-column align="center" label="置顶状态">
          <template slot-scope="scope">
            <span> {{ scope.row.topStatus == 0 ? "未置顶" : "已置顶" }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="topTime" label="置顶截止时间">
          <template slot-scope="scope">
            <span v-if="scope.row.topStatus == 0">-</span>
            <span v-else>{{ scope.row.topTime }}</span>
          </template>
        </el-table-column>

        <el-table-column align="center" prop="visitCount" label="阅读量" />

        <el-table-column
          prop="likeCount"
          align="center"
          label="点赞"
        ></el-table-column>
        <el-table-column
          prop="collectCount"
          align="center"
          label="收藏"
        ></el-table-column>
        <el-table-column align="center" label="评论">
          <template slot-scope="scope">



            <router-link  v-if="scope.row.commentStatus  != 0" :to="{ path: `/article/talk?id=${scope.row.id}` }">
              <span style="cursor: pointer; color: #3a8ee6">{{
                scope.row.commentCount
              }}</span>
            </router-link>
            <span v-else>/</span>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="operator" label="创建人" />
        <el-table-column align="center" prop="createTime" label="创建时间">
        </el-table-column>
        <el-table-column align="center" label="状态">
          <template slot-scope="scope">
            <span
              v-if="scope.row.tasksStatus == 1 && scope.row.publishStatus == 0"
              >暂存，未发布</span
            >
            <span
              v-else-if="
                scope.row.tasksStatus == 2 && scope.row.publishStatus == 0
              "
              >定时待发布</span
            >
            <span
              v-else-if="
                scope.row.tasksStatus == 3 || scope.row.publishStatus == 0
              "
              >取消发布</span
            >
            <span v-else-if="scope.row.publishStatus == 1">已发布</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <!--            <el-button size="mini" type="text">预览</el-button>-->
            <el-button
              v-if="scope.row.publishStatus == 1"
              size="mini"
              type="text"
              @click="handleTopStatus(scope.row)"
              >{{ scope.row.topStatus == 1 ? "取消置顶" : "置顶" }}</el-button
            >
            <el-button
              size="mini"
              type="text"
              @click="changeStatus(scope.row)"
              v-if="scope.row.publishStatus == 1"
              >取消发布</el-button
            >
            <el-button
              v-if="scope.row.publishStatus == 0"
              size="mini"
              type="text"
              @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button size="mini" type="text" @click="handleDetail(scope.row)"
              >预览</el-button
            >
            <el-button
              v-if="
                (scope.row.tasksStatus <= 1 || scope.row.tasksStatus == 4) &&
                  scope.row.publishStatus == 0
              "
              size="mini"
              type="text"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        class="pageRight"
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[10, 20, 30, 40]"
        :current-page="searchForm.pageNum"
        :page-size="searchForm.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableData.total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { pickerOptions } from "../product/options";
import { isGreaterThanCurrentTime } from "@/utils/date";
import {
  getArticleList,
  deleteArticle,
  editArticleStatus,
  deleteArticleBatch,
  getArticleListTree,
  editArticleTop,
  getLinkAccountlist,
  batchPublishArticle
} from "@/api/article";
import Tinymce from "@/components/Tinymce/index.vue";
export default {
  name: "Article",
  components: {
    Tinymce
  },
  data() {
    return {
      props: {
        emitPath: false
      },
      pickerOptions,
      str: "",
      searchForm: {
        topStatus: "",
        linkAccountId: "",
        id: "",
        sourceType: 0,
        publishStatus: "",
        type: 1,
        title: "",
        beginTime: "",
        endTime: "",
        pageNum: 1,
        pageSize: 10,
        cateId: ""
      },
      tableData: {
        data: [],
        total: 0
      },
      listLoading: false,
      deleteList: [],
      cateList: [],
      linkAccountList: []
    };
  },
  async created() {
    await this.getCateList();
    await this.getList();
    await this.getlinkAccountList();
  },
  mounted() {},
  methods: {
    getlinkAccountList() {
      getLinkAccountlist().then(res => {
        this.linkAccountList = res.data;
      });
    },

    changeStatus(row) {
      editArticleStatus({
        articleIdList: [row.id],
        publishStatus: 0
      }).then(res => {
        this.$message({
          type: "success",
          message: "修改成功!"
        });
        this.getList();
      });
    },

    handleTopStatus(v) {
      if (!v.topTime || !isGreaterThanCurrentTime(v.topTime)) {
        this.$router.push({
          path: "/article/add",
          query: {
            id: v.id
          }
        });
      } else {
        editArticleTop(v.id).then(res => {
          if (res.code == 200) {
            this.$message({
              message: "操作成功",
              type: "success"
            });
            this.getList();
          }
        });
      }
    },
    getCateList() {
      getArticleListTree().then(res => {
        if (res.code == 200) {
          this.cateList = res.data;
        }
      });
    },
    resetForm(form) {
      // this.$refs[form].resetFields();
      this.searchForm = {
        topStatus: "",
        linkAccountId: "",
        id: "",
        publishStatus: "",
        sourceType: 0,
        type: 1,
        searchForm: 0,
        title: "",
        beginTime: "",
        endTime: "",
        pageNum: 1,
        pageSize: 10,
        cateId: ""
      };
      this.getList();
    },
    selectTableList(val) {
      this.deleteList = val.map(v => v.id);
    },
    selecTableItem(row) {
      if (row.publishStatus === 0) {
        return true;
      }
    },
    deleteMany() {
      let that = this;
      if (this.deleteList.length == 0) {
        this.$message({
          message: "请先选择要删除的项",
          type: "warning"
        });
      } else {
        this.$confirm("是否确认删除?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          deleteArticleBatch(this.deleteList.join(",")).then(res => {
            if (res.code == 200) {
              that.$message({
                type: "success",
                message: "删除成功!"
              });
              setTimeout(() => {
                that.getList();
              }, 500);
            }
          });
        });
      }
    },

    handleEdit(v) {
      this.$router.push({
        path: "/article/add",
        query: {
          id: v.id
        }
      });
    },
    handleDelete(v) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        deleteArticle(v.id).then(res => {
          this.$message({
            type: "success",
            message: "删除成功!"
          });
          this.getList();
        });
      });
    },
    handleDetail(v) {
      this.$router.push({
        path: "/article/detail",
        query: {
          id: v.id
        }
      });
    },
    handleSizeChange(val) {
      this.searchForm.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.searchForm.pageNum = val;
      this.getList();
    },
    addArtice() {
      this.$router.push({
        path: "/article/add"
      });
    },
    getList() {
      this.listLoading = true;
      getArticleList(this.searchForm)
        .then(res => {
          this.tableData.data = res.data.list;
          this.tableData.total = res.data.total;
          this.listLoading = false;
        })
        .finally(() => {
          this.listLoading = false;
        });
    }
  }
};
</script>
<style lang="scss" scoped>
.app-container {
  height: calc(100vh - 50px);
  background-color: #f5f5f5;
  position: relative;
}
</style>
